package org.tinyweb.render;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tinyweb.core.Path;
import org.tinyweb.core.TinyWeb;
import org.tinyweb.core.ViewType;


public class RenderFactory {
    private static final Logger LOGGER = LoggerFactory.getLogger(RenderFactory.class);
    private static final RenderFactory renderFactory = new RenderFactory();
    private static String defaultViewType = ViewType.JSP;
    
    private RenderFactory(){
        
    }

    public static RenderFactory instance(){
        return renderFactory;
    }

    public void init(String encoding,String viewType){
        Render.init(encoding);
        defaultViewType = viewType;
        initBeetlRender();
    }
    
    private void initBeetlRender(){
        try {
            Class.forName("org.beetl.core.GroupTemplate");
            TinyWeb tinyWeb = TinyWeb.instance();
            BeetlRender.init(tinyWeb.getPath().getWebAppRealPath());
        } catch (ClassNotFoundException e) {
            LOGGER.error("找不到beetl，beetl初始化失败！", e);
        }
    }
    
    public static Render getRender(String view){
        if("beetl".equals(defaultViewType)){
            return new BeetlRender(view);
        } else {
            return new NullRender();
        }
    }
    
    public Render getBeetlRender(String view){
        return new BeetlRender(view);
    }
    
    public Render getJspRender(String view){
        return new JspRender(view);
    }
    
    public Render getNullRender(){
        return new NullRender();
    }
    
    public Render getHtmlRender(String text){
        return new HtmlRender(text);
    }
    
    public Render getJavascriptRender(String jsText){
        return new JavascriptRender(jsText);
    }
    
    public Render getTextRender(String text){
        return new TextRender(text);
    }
    
    public Render getTextRender(String text,String contentType){
        return new TextRender(text,contentType);
    }
    
    public Render getJsonRender(String jsonText){
        return new JsonRender(jsonText);
    }
    
    public Render getRedirectRender(String url){
        return new RedirectRender(url);
    }
    
}
